<?php
header('Content-Type:text/html;charset=utf-8 ');
require_once __DIR__ . '/vendor/' . '/autoload.php';
define("notify_url","http://49.235.220.167:8111/chinapay/notify.php");
define("return_url","http://49.235.220.167:8111/chinapay/return.php");
/**
 * 支付
 * @return string
 */
function pay()
{
    $merId = "000092012257647";
    $orderNo = date("YmdHis") . mt_rand(100, 999);
    $propFile = __DIR__ . '/src/security.ini';
    $tranDate = date('Ymd');
    $tranTime = date('His');
    $OrderAmt = 100;  // 单位分
    $TranType = "0005";
    $BusiType = "0001";
    $MerPageUrl = return_url;
    $MerBgUrl = notify_url;
    $RemoteAddr = "127.0.0.1";

    try {
        $PayContentBuilder = new \ChinaPay\Payment\PayContentBuilder();
        $PayContentBuilder->setSecurityPropFile($propFile);
        $PayContentBuilder->setMerId($merId);
        $PayContentBuilder->setMerOrderNo($orderNo);
        $PayContentBuilder->setTranDate($tranDate);
        $PayContentBuilder->setTranTime($tranTime);
        $PayContentBuilder->setOrderAmt($OrderAmt);
        $PayContentBuilder->setTranType($TranType);
        $PayContentBuilder->setBusiType($BusiType);
        $PayContentBuilder->setMerPageUrl($MerPageUrl);
        $PayContentBuilder->setMerBgUrl($MerBgUrl);
        $PayContentBuilder->setRemoteAddr($RemoteAddr);
        $content = $PayContentBuilder->getBizContent();
        $res = \ChinaPay\Factory::payment()->testBgPay()->buildRequestForm($content);
        die($res);
//    parse_str($res, $arr);
//    print_r($arr);
    } catch (\ChinaPay\Exception\ChinaPayException $exception) {
        return urldecode($exception->getMessage());
    }
}

/**
 * 回调
 */
function notify()
{
    $post = $_POST;
    $propFile = __DIR__ . '/src/security.ini';
    $PayContentBuilder = new \ChinaPay\Payment\PayContentBuilder();
    $PayContentBuilder->setSecurityPropFile($propFile);
    $result = $PayContentBuilder->paymentVerify($post);
    if ($result) {
        // 验签成功
    }
}



